#include "speed.h"

Speed::Speed()
{
	displayWidth = Iw2DGetSurfaceWidth();
	displayHeight = Iw2DGetSurfaceHeight();

	timeUnSpawned = 0;
	onStage = false;
	readyToSpawn = false;

	position = Vector2(-1000, -1000);

	SetupImages();

	boundingBox = BoundingBox(position, width, height);
}

Speed::~Speed()
{
	CleanupImages();
}

void Speed::SetupImages()
{
	speedSprite = Iw2DCreateImageResource("speed");
	width  = speedSprite->GetWidth();
    height = speedSprite->GetHeight();
	widthHalf = width / 2;
	heightHalf = height / 2;
}

void Speed::CleanupImages()
{
	if(speedSprite != NULL)
	{
		delete speedSprite;
		speedSprite = NULL;
	}
}

void Speed::Render()
{
	Iw2DDrawImage(speedSprite, CIwSVec2((int16)position.x - widthHalf, (int16)position.y - heightHalf));
}

void Speed::Update(int deltaTimeMs)
{
	timeUnSpawned += deltaTimeMs;

	if(onStage == false  && timeUnSpawned > SPEED_TIME)
	{
		float random = Random::Instance()->RandomFloatZeroToOne();
		if(random * ((timeUnSpawned - SPEED_TIME)/1000) > 5)
		{
			readyToSpawn = true;
		}
	}
}

void Speed::Spawn()
{
	position = Vector2(Random::Instance()->RandomIntBetween(0, displayWidth), Random::Instance()->RandomIntBetween(0, displayHeight));
	boundingBox.UpdatePosition(position);

	onStage = true;
	readyToSpawn = false;
}

void Speed::UnSpawn()
{
	position = Vector2(-1000, -1000);
	boundingBox.UpdatePosition(position);

	timeUnSpawned = 0;
	onStage = false;
}